import { defineStore } from 'pinia'
import {
	getComments,
	getPosts,
	publishPost,
	removeComment,
	removePost,
	updatePost,
	searchFavorityUsers,
	searchLikeUsers,
	likePost,
	collectPost,
	publishComment,
	replyComment,
	queryMyCollection,
	queryMyPosts,
	queryPostsByUserId
} from '@/service/post.js'
import { getPostById } from '../service/post'
const usePostStore = defineStore('post', {
	state: () => ({
		postsList: [],
		post: {},
		postsTotalCount: 0,
		commentList: [],
		postId: 0,
		favUsers: [],
		likeUsers: [],
		myPosts: [],
		userPost: []
	}),
	actions: {
		// 发布帖子
		async publishPostAction(postInfo) {
			await publishPost(postInfo)
		},
		// 删除帖子
		async removePostAction(post_id) {
			await removePost(post_id)
			this.queryMyPostsAction()
		},
		// 修改帖子
		async updatePostAction(post_id, postInfo) {
			await updatePost(post_id, postInfo)
		},
		// 查询帖子
		async getPostsAction(queryInfo) {
			const res = await getPosts(queryInfo)
			const { list, totalCount } = res.data
			this.postsList = list
			this.postsTotalCount = totalCount
		},
		// 根据id查询帖子
		async getPostByIdAction(post_id) {
			const res = await getPostById(post_id)
			this.post = res.data[0]
		},

		// 查询评论
		async searchCommentByIdAction(post_id) {
			const res = await getComments(post_id)
			this.commentList = res.data
			this.postId = post_id
		},
		// 发布评论
		async publishCommentAction(post_id, content) {
			await publishComment(post_id, content)
			this.searchCommentByIdAction(this.postId)
		},
		
		// 回复评论
		async replyCommentAction(post_id, content, parent_id) {
			await replyComment(post_id, content, parent_id)
			this.searchCommentByIdAction(this.postId)
		},
		
		// 删除评论
		async removeCommentByIdAction(comment_id) {
			const res = await removeComment(comment_id)
			this.searchCommentByIdAction(this.postId)
		},
		// 查询收藏的人
		async searchFavorityUsersAction(post_id) {
			const res = await searchFavorityUsers(post_id)
			this.favUsers = res.data
		},
		// 查询点赞的人
		async searchLikeUsersAction(post_id) {
			const res = await searchLikeUsers(post_id)
			this.likeUsers = res.data
		},
		// async 点赞帖子
		async likePostAction(post_id) {
			const res = await likePost(post_id)
			this.searchLikeUsersAction(post_id)
		},
		// 收藏帖子
		async collectPostAction(post_id) {
			const res = await collectPost(post_id)
			this.searchFavorityUsersAction(post_id)
		},
		// 查看收藏的帖子
		async queryMyCollectionAction() {
			const res = await queryMyCollection()
			const { list, totalCount } = res.data
			this.postsList = list
			this.postsTotalCount = totalCount
		},
		// 查看自己发布的帖子
		async queryMyPostsAction() {
			const res = await queryMyPosts()
			this.myPosts = res.data
		},
		async queryPostsByUserIdAction(user_id) {
			const res = await queryPostsByUserId(user_id)
			this.userPost = res.data
		}
	}
})

export default usePostStore
